Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow graphql pretty formatted files #466

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

p-makowski
Copy link

@p-makowski p-makowski commented Sep 13, 2023

Currently checks for GraphQL ValidTypeName assumes that type definitions are always one-liners.
For a better readability it is much better to format GraphQL files so that they are multiline.

type CartCampaign
    @doc(
        description: "CartCampaign returns the discount amount set in the quote"
    ) {
    full_discount_amount: Float
}

instead of

type CartCampaign @doc(description: "CartCampaign returns the discount amount set in the quote") {
    full_discount_amount: Float
}

There can many other tags like @doc and in current implementation all of them have to be in one line together with type to satisfy sniff. Changes in this PR will make pretty version to pass checks.

\PHP_CodeSniffer\Tokenizers\GRAPHQL::tokenize adds \n to token content anyway (see vendor/magento/magento-coding-standard/PHP_CodeSniffer/Tokenizers/GRAPHQL.php:134) to not screw up with line numbers.

This PR Closes #465

@@ -32,7 +32,7 @@ public function process(File $phpcsFile, $stackPtr)
//compose entity name by making use of the next strings that we find until we hit a non-string token
$name = '';
for ($i=$stackPtr+1; $tokens[$i]['code'] === T_STRING; ++$i) {
$name .= $tokens[$i]['content'];
$name .= rtrim($tokens[$i]['content']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to also allow white-space on the left of the name?

Suggested change
$name .= rtrim($tokens[$i]['content']);
$name .= trim($tokens[$i]['content']);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that tokenizer is never gonna create a T_STRING token with a whitespace on the left.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should either fix the tokeniser to never include (leading nor) trailing whitespace in a T_STRING token, or run this through trim(). Using rtrim() seems incomplete.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation and highlighting where the extra white-space comes from (ie, the GraphQL tokeniser within this repository). Let's add a code comment here so others know why removing the trailing newline character(s) is safe in this context.

@fredden
Copy link
Member

fredden commented Sep 14, 2023

@p-makowski please can you use a closing keyword for #465 here.

@p-makowski
Copy link
Author

@fredden I added issue id in my commits, so I believe issue and PR are linked already.
But sure, I can add it in case it is not enough.

@p-makowski
Copy link
Author

Closes #465

@fredden
Copy link
Member

fredden commented Sep 19, 2023

I added issue id in my commits, so I believe issue and PR are linked already. But sure, I can add it in case it is not enough.
Closes #465

Please can you put this comment into the pull request description. In order for GitHub to work its magic, the closing keyword needs to be in the commit message (not just a reference to the issue, but a closing keyword), or the pull request description (not just a comment).

@p-makowski
Copy link
Author

p-makowski commented Sep 19, 2023

@fredden It was unclear to me when I read the documentation if it is about description, comments, commits...
Thank you for clarification. I edited PR description, but I am still unsure if it is linked.

Edit: OK, I see now that it is linked (there is a label when I hover over the line I added to the descrpition)

@fredden
Copy link
Member

fredden commented Sep 19, 2023

Great, thanks for your help on this @p-makowski. I can see that GitHub has linked these up, so when this pull request gets merged in, the issue will be automatically closed.

Screenshot_2023-09-19_15-46-50

Copy link
Member

@fredden fredden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is instead a bug in the tokeniser.

@@ -32,7 +32,7 @@ public function process(File $phpcsFile, $stackPtr)
//compose entity name by making use of the next strings that we find until we hit a non-string token
$name = '';
for ($i=$stackPtr+1; $tokens[$i]['code'] === T_STRING; ++$i) {
$name .= $tokens[$i]['content'];
$name .= rtrim($tokens[$i]['content']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should either fix the tokeniser to never include (leading nor) trailing whitespace in a T_STRING token, or run this through trim(). Using rtrim() seems incomplete.

@p-makowski
Copy link
Author

p-makowski commented Oct 25, 2023

I wonder if this is instead a bug in the tokeniser.

@fredden see the last sentence in PR description:

\PHP_CodeSniffer\Tokenizers\GRAPHQL::tokenize adds \n to token content anyway (see vendor/magento/magento-coding-standard/PHP_CodeSniffer/Tokenizers/GRAPHQL.php:134) to not screw up with line numbers.

EOL char is added there on purpose "otherwise PHP_CodeSniffer will screw up line numbers".
So it looks like it was educated decision.

Regarding trim vs. rtrim - I am not sure if I want to open additional space for regression bugs.
I just do not have enough experience with coding standards to figure out all scenarios of what I can possibly break by trimming also on the beginning of the string. My gut feeling is that it should be OK, but on the other hand it does not relate anyhow to the specific issue that this PR solves. So the question is if it is worth the risk?

Copy link
Member

@fredden fredden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick reply. I did miss that line in the pull request description, so thanks for highlighting it. I've reviewed the tokeniser code now, and using rtrim() makes sense to me now.

@@ -32,7 +32,7 @@ public function process(File $phpcsFile, $stackPtr)
//compose entity name by making use of the next strings that we find until we hit a non-string token
$name = '';
for ($i=$stackPtr+1; $tokens[$i]['code'] === T_STRING; ++$i) {
$name .= $tokens[$i]['content'];
$name .= rtrim($tokens[$i]['content']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation and highlighting where the extra white-space comes from (ie, the GraphQL tokeniser within this repository). Let's add a code comment here so others know why removing the trailing newline character(s) is safe in this context.

@p-makowski
Copy link
Author

Code comment has been added @fredden

@p-makowski
Copy link
Author

p-makowski commented Dec 1, 2023

Hi @fredden , any ETA on getting it out?

@fredden
Copy link
Member

fredden commented Dec 6, 2023

@p-makowski I have no control over Adobe's release process.

@p-makowski
Copy link
Author

@p-makowski I have no control over Adobe's release process.

Oh, sorry @fredden! Somehow I was sure that you are part of maintaining team

@p-makowski
Copy link
Author

@sidolov When can we get any attention and hopefully a release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Reviewer Approved
Development

Successfully merging this pull request may close these issues.

Warnings on well formatted GraphQL files
5 participants